package cn.wmelon.shell.command;

import cn.wmelon.shell.command.script.builder.ScriptBuilder;

/**
 * 一个简单的jar启动脚本
 *
 * @author watermelon
 * @time 2020/3/4
 */
public class StartShCommand extends AbstractCommand {


    public StartShCommand(String projectPath, String jarName, String profiles) {
        this.projectPath = projectPath;
        this.jarName = jarName;
        this.profiles = profiles;
    }

    /**
     * 项目地址
     */
    private String projectPath;

    /**
     * 启动的Jar名字
     */
    private String jarName;

    /**
     * 启动环境
     */
    private String profiles;


    @Override
    String commandContent() {
        //构建一个 能生成一个脚本文件 的字符串
        ScriptBuilder scriptbuilder = new ScriptBuilder()
                .addText("projectPath=").addTextLn(projectPath)
                .addText("jarName=").addTextLn(jarName)
                .addText("profiles=").addTextLn(profiles)
                .addTextLn("echo \"---------------------------开始启动服务-----------------------------------\"")
                .addTextLn("echo \"---------------------------启动环境: $profiles\"")
                .addTextLn("ps -ef | grep $jarName | awk '\"'\"'{print $2}'\"'\"' | xargs kill -9")
                .addTextLn("echo \"---------------------------kill完毕，开始启动-----------------------------\"")
                .addTextLn("if [ ! -d $projectPath/logs/ ];then")
                .addTextLn(" mkdir $projectPath/logs")
                .addTextLn("fi")
                .addTextLn("nohup java -jar $projectPath/$jarName --spring.profiles.active=$profiles >$projectPath/logs/startUp.log 2>&1 &");

        //构建要执行的命令
//        String shellBuilder = new ShellBuilder().aDragon(scriptbuilder).build();
        String shellBuilder = this.builder.createScript(scriptbuilder).chmodXScript(scriptbuilder).build();
        return shellBuilder;
    }
}
